import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import pipeline.MysqlTestPipeline;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.selenium.SeleniumDownloader;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.sql.SQLException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CrawlerTest implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);

    @Override
    public void process(Page page) {
        System.out.println("输出：");
        String taskId = UUID.randomUUID().toString();
//        String name = page.getHtml().toString();
//        System.out.println(name);
        Selectable stable = page.getHtml().$("div.hotel_new_list");
        List<Map<String,Object>> infos = new ArrayList<Map<String,Object>>();
        stable.nodes().forEach(subs -> prepareMap(infos,subs,taskId));
        page.putField("infos",infos);

    }

    private void prepareMap(List<Map<String,Object>> infos,Selectable stable,String taskId){
        Map<String,Object> infoMap = new HashMap<String,Object>();
        //存入地址信息
        infoMap.put("address",stable.$("p.hotel_item_htladdress").regex("\\】(.*?)\\。",1).toString());
        infoMap.put("name",stable.$("a.hotel_item_pic","title").toString());
        infoMap.put("minValue",stable.$("span.J_price_lowList","text").toString());
        infoMap.put("taskId",taskId);
        infoMap.put("createTime",new Date());
        infos.add(infoMap);


    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[]  args) {
//        System.getProperties().setProperty("webdriver.chrome.driver","/home/bruce/soft/chromedriver");
//        WebDriver webDriver = new ChromeDriver();
//        webDriver.get("https://hotels.ctrip.com/hotel/beijing1#ctm_ref=hod_hp_sb_lst");
//        WebElement webElement = webDriver.findElement(By.xpath("/html"));
//        System.out.println(webElement.getAttribute("outerHTML"));
//        webDriver.close();
          System.setProperty("selenuim_config","/home/bruce/soft/config.ini");
        try {
            Spider.create(new CrawlerTest())
                    .addUrl("https://hotels.ctrip.com/hotel/beijing1#ctm_ref=hod_hp_sb_lst")
                    .addPipeline(new MysqlTestPipeline())
                    .setDownloader(new SeleniumDownloader("/home/bruce/soft/chromedriver"))
                    .thread(1)
                    .run();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
